/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.polimi.deib.se2.meteocal.business.boundary;

import it.polimi.deib.se2.meteocal.business.control.Utils;
import it.polimi.deib.se2.meteocal.business.entity.Event;
import it.polimi.deib.se2.meteocal.business.entity.Invitation;
import it.polimi.deib.se2.meteocal.business.entity.Notification;
import it.polimi.deib.se2.meteocal.business.entity.User;
import java.security.Principal;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class NotificationManager {
    
    @PersistenceContext
            EntityManager em;
    
    @Inject
            Principal principal;
    
    public void save(Notification notification) {
        em.persist(notification);
    }
    
    public Notification find(Long id) {
        return em.find(Notification.class, id);
    }
    
    public void remove(Notification notification){
        em.remove(em.merge(notification));
    }
    
    public List<Notification> getNotificationsByUser(User loggedUser) {
        Query q = em.createNativeQuery("SELECT * FROM meteocaldb.NOTIFICATIONS WHERE userPinged=\""+loggedUser.getEmail()+"\"",Notification.class);
        return q.getResultList();
    }
    
    public List<Notification> getNotificationsByEvent(Event event) {
        Query q = em.createNativeQuery("SELECT * FROM meteocaldb.NOTIFICATIONS WHERE referingEvent=\""+ event.getId() +"\"", Notification.class);
        return q.getResultList();
    }
    
    
    public boolean findByDetailsAndReferingEvent(Event event, String details) {
        Query q = em.createNativeQuery("SELECT * FROM meteocaldb.NOTIFICATIONS WHERE (referingEvent=\""+event.getId()+"\" AND DETAILS=\""+details+"\")");
        return q.getResultList().isEmpty();
    }
    
    public Notification sendNotification(User userPinged, Event event, Invitation invitation,String details){
//        User userPinged = getUm().find(pinged);
        Notification notification = new Notification();
        notification.setId(System.currentTimeMillis());
        notification.setDetails(details);
        Date notificationDate = new Date();
        notification.setDate(notificationDate);
        notification.setUserPinged(userPinged);
        notification.setReferingEvent(event);
        notification.setInvitation(invitation);
        
        userPinged.getNotifications().add(notification);
        event.getNotifications().add(notification);
        
        /*nm.*/save(notification);
        
//        if(invitation != null){
//            invitation.setNotification_id(notification);
//            im.update(invitation);
//        }
        
        Utils.prepareMail(details, userPinged.getEmail(), userPinged.getName());
        return notification;
    }
}
